package com.example.a_java.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.dto.RoleDTO;
import com.example.a_java.entity.SysRole;
import com.example.a_java.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/system/role")
@SecurityRequirement(name = "bearer-key")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Operation(summary = "获取角色列表", description = "分页获取角色列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:role:list')")
    public Result<Page<SysRole>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "角色名称") @RequestParam(required = false) String roleName,
            @Parameter(description = "权限字符") @RequestParam(required = false) String roleKey,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<SysRole>()
                .like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName)
                .like(StringUtils.hasText(roleKey), SysRole::getRoleKey, roleKey)
                .eq(status != null, SysRole::getStatus, status)
                .orderByAsc(SysRole::getSortOrder);
        
        roleService.page(page, wrapper);
        return Result.success(page);
    }

    @Operation(summary = "获取角色详情")
    @GetMapping("/{roleId}")
    @PreAuthorize("hasAuthority('system:role:query')")
    public Result<RoleDTO> getInfo(@Parameter(description = "角色ID") @PathVariable Long roleId) {
        RoleDTO role = roleService.getRoleInfo(roleId);
        return Result.success(role);
    }

    @Operation(summary = "新增角色")
    @PostMapping
    @PreAuthorize("hasAuthority('system:role:add')")
    public Result<Boolean> add(@RequestBody @Valid RoleDTO roleDTO) {
        return Result.success(roleService.createRole(roleDTO));
    }

    @Operation(summary = "修改角色")
    @PutMapping
    @PreAuthorize("hasAuthority('system:role:edit')")
    public Result<Boolean> update(@RequestBody @Valid RoleDTO roleDTO) {
        return Result.success(roleService.updateRole(roleDTO));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{roleId}")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public Result<Boolean> delete(@PathVariable Long roleId) {
        return Result.success(roleService.deleteRole(roleId));
    }

    @Operation(summary = "分配角色菜单权限")
    @PutMapping("/menu")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public Result<Boolean> assignMenu(@RequestBody RoleDTO roleDTO) {
        return Result.success(roleService.assignRoleMenu(roleDTO.getRoleId(), roleDTO.getMenuIds()));
    }

    @Operation(summary = "获取角色菜单列表")
    @GetMapping("/menu/{roleId}")
    @PreAuthorize("hasAuthority('system:role:query')")
    public Result<List<Long>> getRoleMenuList(@PathVariable Long roleId) {
        List<Long> menuIds = roleService.getRoleMenuIds(roleId);
        return Result.success(menuIds);
    }
} 
